<?php

namespace common\models\goods;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\goods\Goods;

/**
 * GoodsSearch represents the model behind the search form about `common\models\goods\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'category_id', 'goods_type', 'brand_id', 'is_on_sale', 'is_del', 'add_time', 'last_update', 'supplier_id', 'is_stock_disable', 'is_auto_sale_on', 'is_auto_sale_off', 'stock','factory_id'], 'integer'],
            [['name', 'img', 'remark', 'pack_unit', 'class_id'], 'safe'],
            [['price_retail', 'price_mix', 'price_pack'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }
    
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '商品名称',
            'category_id' => '所属类目',
            'goods_type' => '商品类型',
            'brand_id' => '商品品牌',
            'img' => '商品图片',
            'is_on_sale' => '销售状态',
            'is_del' => '已删除',
            'add_time' => '添加时间',
            'last_update' => '最后编辑时间',
            'remark' => '备注',
            'supplier_id' => '所属供应商',
            'pack_unit' => '包装单位',
            'class_id' => '分类',
            'price_retail' => '零售价',
            'price_mix' => '混批价',
            'price_pack' => '整箱价',
            'is_stock_disable' => '不检测商品库存',
            'is_auto_sale_on' => '自动上架',
            'is_auto_sale_off' => '自动下架',
            'stock' => '商品总数',
            'factory_id' => '厂家',
        ];
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        
        $query->andFilterWhere([
            'id' => $this->id,
            'category_id' => $this->category_id,
            'goods_type' => $this->goods_type,
            'brand_id' => $this->brand_id,
            'is_on_sale' => $this->is_on_sale,
            'is_del' => $this->is_del,
            'add_time' => $this->add_time,
            'last_update' => $this->last_update,
            'supplier_id' => $this->supplier_id,
            'price_retail' => $this->price_retail,
            'price_mix' => $this->price_mix,
            'price_pack' => $this->price_pack,
            'is_stock_disable' => $this->is_stock_disable,
            'is_auto_sale_on' => $this->is_auto_sale_on,
            'is_auto_sale_off' => $this->is_auto_sale_off,
            'stock' => $this->stock,
            'factory_id' => $this->factory_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            //->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'remark', $this->remark])
            ->andFilterWhere(['like', 'pack_unit', $this->pack_unit]);
            //->andFilterWhere(['like', 'concat(",",class_id,",")', ',21,']);
        if(!empty($this->class_id)){
            $class=['or'];
            $ids = explode(',', $this->class_id);
            foreach ($ids as $id) {
                $class[]=['like', 'class_id', ",{$id},"];
            }
            $query->andFilterWhere($class);
        }

        return $dataProvider;
    }
}
